{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def longestConsecutive(self, nums: List[int]) -> int:\n",
    "        max_len = 0\n",
    "        # 通过set去重\n",
    "        nums = set(nums)\n",
    "        # 遍历左边界\n",
    "        for i in nums:\n",
    "            # 首先寻找序列的起点\n",
    "            if i - 1 not in nums:\n",
    "                cur_num = i\n",
    "                cur_len = 1\n",
    "                # 向后验证是否连续\n",
    "                while cur_num + 1 in nums:\n",
    "                    cur_num += 1\n",
    "                    cur_len += 1\n",
    "                max_len = max(max_len, cur_len)\n",
    "        return max_len\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    nums = [0,3,7,2,5,8,4,6,0,1]\n",
    "    print(Solution().longestConsecutive(nums))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pylearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
